package cn.iocoder.yudao.module.eval.controller.admin.candidate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
// import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.eval.controller.admin.candidate.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.candidate.EvalCandidateInfoDO;
import cn.iocoder.yudao.module.eval.service.candidate.EvalCandidateInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
// import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 参评人员信息")
@RestController
@RequestMapping("/eval/candidate-info")
@Validated
public class EvalCandidateInfoController {

    @Resource
    private EvalCandidateInfoService candidateInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建参评人员信息")
    @PreAuthorize("@ss.hasPermission('eval:candidate-info:create')")
    public CommonResult<Long> createEvalCandidateInfo(@Valid @RequestBody EvalCandidateInfoSaveReqVO createReqVO) {
        return success(candidateInfoService.createCandidateInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参评人员信息")
    @PreAuthorize("@ss.hasPermission('eval:candidate-info:update')")
    public CommonResult<Boolean> updateEvalCandidateInfo(@Valid @RequestBody EvalCandidateInfoSaveReqVO updateReqVO) {
        candidateInfoService.updateCandidateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参评人员信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate-info:delete')")
    public CommonResult<Boolean> deleteEvalCandidateInfo(@RequestParam("id") Long id) {
        candidateInfoService.deleteCandidateInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参评人员信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:candidate-info:query')")
    public CommonResult<EvalCandidateInfoRespVO> getEvalCandidateInfo(@RequestParam("id") Long id) {
        EvalCandidateInfoDO candidate = candidateInfoService.getCandidateInfo(id);
        return success(BeanUtils.toBean(candidate, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参评人员信息分页")
    @PreAuthorize("@ss.hasPermission('eval:candidate-info:query')")
    public CommonResult<PageResult<EvalCandidateInfoRespVO>> getEvalCandidateInfoPage(@Valid EvalCandidateInfoPageReqVO pageReqVO) {
        PageResult<EvalCandidateInfoDO> pageResult = candidateInfoService.getCandidateInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参评人员信息 Excel")
    @PreAuthorize("@ss.hasPermission('eval:candidate-info:export')")
    // @OperateLog(type = EXPORT)
    public void exportEvalCandidateInfoExcel(@Valid EvalCandidateInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalCandidateInfoDO> list = candidateInfoService.getCandidateInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参评人员信息.xls", "数据", EvalCandidateInfoRespVO.class,
                        BeanUtils.toBean(list, EvalCandidateInfoRespVO.class));
    }

}
